package com.myjob.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myjob.config.Constants;
import com.myjob.entity.Advertisement;
import com.myjob.mapper.AdvertisementMapper;
import com.myjob.response.Result;
import com.myjob.service.AdvertisementService;
import com.myjob.utils.UtilValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @author Chen Yuan
 */

@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement> implements AdvertisementService {

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Override
    public Result selectAdvertise(Integer currentPage, Integer pageSize, String level, String searchValue) {
        Page page = null;
        QueryWrapper<Advertisement> wrapper = new QueryWrapper<>();

        if (UtilValidate.isNotEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (UtilValidate.isNotEmpty(searchValue)) {
            wrapper.and(w -> w.like("level", searchValue)
                    .or().like("company_name", searchValue));
        }
        wrapper.orderByDesc("create_date");

        if (UtilValidate.isEmpty(pageSize)){
            page = new Page(currentPage, Constants.getPageSize());
        }else {
            page = new Page(currentPage, pageSize);
        }

        IPage iPage = advertisementMapper.selectPage(page, wrapper);
        return Result.succeed(iPage);
    }
}